home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / BigInt.h < prev    next >
C/C++ Source or Header  |  1990-05-15  |  2KB  |  59 lines

  1. // BigInt.h -- Multiple-precision integer class
  2.  
  3. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/BigInt.h,v 3.0 90/05/15 22:43:27 kgorlen Rel $
  4.  
  5. #include <stdio.h>
  6. #include <iostream.h>
  7.  
  8. class BigInt {
  9.     char* digits;           // pointer to digit array in free store
  10.     unsigned ndigits;       // number of digits
  11.     BigInt(char* d, unsigned n) {   // constructor function
  12.         digits = d;
  13.         ndigits = n;
  14.     }
  15.     friend DigitStream;
  16.     char* scanChunk(istream&);      // read a chunk of digits
  17. public:
  18.     BigInt(const char*);            // constructor function
  19.     BigInt(unsigned n =0);          // constructor function
  20.     BigInt(const BigInt&);          // copy constructor function
  21.     void operator=(const BigInt&);  // assignment
  22.     BigInt operator+(const BigInt&) const;  // addition operator
  23.                                             // function
  24.     void print(FILE* f =stdout) const;      // printing function
  25.     ~BigInt()   { delete digits; }          // destructor function
  26.     void printOn(ostream&) const;           // printing function
  27.     void scanFrom(istream&);                // reading function
  28. };
  29.  
  30. inline istream& operator>>(istream& strm, BigInt& b)
  31. {
  32.     b.scanFrom(strm);
  33.     return strm;
  34. }
  35.  
  36. inline ostream& operator<<(ostream& strm, const BigInt& b)
  37. {
  38.     b.printOn(strm);
  39.     return strm;
  40. }
  41.  
  42. class DigitStream {
  43.     char* dp;               // pointer to current digit
  44.     unsigned nd;            // number of digits remaining
  45. public:
  46.     DigitStream(const BigInt& n) {  // constructor function
  47.         dp = n.digits;
  48.         nd = n.ndigits;
  49.     }
  50.     unsigned operator++() {         // return current digit
  51.                                     // and advance
  52.         if (nd == 0) return 0;
  53.         else {
  54.             nd--;
  55.             return *dp++;
  56.         }
  57.     }
  58. };
  59.